<template>
  <div class="left-panel">
    <div class="form-container" v-for="item in config" :key="item.title">
      <div class="box">
        <div class="category-header">
          <span class="title">{{ item.title }}</span>
          <div class="category-badge">{{ item.config.length }}</div>
        </div>
        <draggable
          class="component-list"
          :list="item.config"
          :group="{ name: 'form', pull: 'clone', put: false }"
          :clone="defaultClone"
          itemKey="type"
        >
          <template #item="{ element }">
            <div class="component-item" @click="add(element)">
              <div class="item-content">
                <el-icon class="component-icon">
                  <component :is="element.icon" />
                </el-icon>
                <span class="component-name">{{ element.name }}</span>
              </div>
            </div>
          </template>
        </draggable>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineEmits } from "vue";
import draggable from "vuedraggable";
import { DArrowRight } from '@element-plus/icons-vue'
import config from "./config/element";

const defaultClone = (item) => {
  return JSON.parse(JSON.stringify(item));
};

const emits = defineEmits(["clickAdd"]);

const add = (element) => {
  emits("clickAdd", JSON.parse(JSON.stringify(element)));
};
</script>

<style lang="scss" scoped>
.left-panel {
  height: calc(100vh - 104px);
  background-color: #fff;
  overflow-y: auto;
  padding: 16px;
  border-radius: 8px;
  position: sticky;
  top: 0;
  width: 280px;

  &::-webkit-scrollbar {
    width: 4px;
    height: 4px;
  }

  &::-webkit-scrollbar-thumb {
    background: #e0e3e9;
    border-radius: 2px;
    &:hover {
      background: #c0c4cc;
    }
  }

  &::-webkit-scrollbar-track {
    background: transparent;
  }

  &:hover {
    &::-webkit-scrollbar-thumb {
      background: #c0c4cc;
    }
  }
}

.form-container {
  .box {
    margin-bottom: 24px;
    background: #fff;
    border-radius: 8px;
    &:last-child {
      margin-bottom: 0;
    }
  }
}

.category-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid #f0f2f5;

  .title {
    font-size: 15px;
    font-weight: 600;
    color: #1f2937;
    position: relative;
    padding-left: 12px;

    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 3px;
      height: 16px;
      background: #409EFF;
      border-radius: 2px;
    }
  }

  .category-badge {
    padding: 2px 8px;
    background: #f0f7ff;
    color: #409EFF;
    border-radius: 10px;
    font-size: 12px;
    font-weight: 500;
  }
}

.component-list {
  padding: 8px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.component-item {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  padding: 12px;
  background: #f8fafc;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  cursor: grab;
  transition: all 0.3s ease;
  height: 46px;

  &:hover {
    border-color: #409EFF;
    background: #f0f7ff;
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(64, 158, 255, 0.1);

    .component-icon {
      color: #409EFF;
    }

    .component-name {
      color: #409EFF;
    }
  }

  &:active {
    cursor: grabbing;
  }

  .item-content {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
  }

  .component-icon {
    font-size: 18px;
    color: #606266;
    transition: all 0.3s ease;
    flex-shrink: 0;
  }

  .component-name {
    font-size: 13px;
    color: #606266;
    transition: all 0.3s ease;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

// 拖拽时的样式
.sortable-ghost {
  opacity: 0.5;
  background: #f0f7ff;
  border: 1px dashed #409EFF;
}

.sortable-drag {
  opacity: 0.9;
  background: #fff;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

// 响应式布局
@media screen and (max-width: 1400px) {
  .left-panel {
    width: 240px;
    
    .component-item {
      padding: 10px;
      height: 42px;
      
      .component-icon {
        font-size: 16px;
      }
      
      .component-name {
        font-size: 12px;
      }
    }
  }
}

@media screen and (max-width: 768px) {
  .left-panel {
    width: 200px;
    height: calc(100vh - 144px);
  }
  
  .component-list {
    grid-template-columns: repeat(1, 1fr);
  }
}
</style>
  